package com.delaphone.flocash.web.action;

import java.util.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.opensymphony.xwork2.Action;
import com.delaphone.flocash.model.FBillServices;
import com.delaphone.flocash.model.FOrders;
import com.delaphone.flocash.service.PaymentService;
import com.delaphone.flocash.util.StringUtil;
import com.delaphone.flocash.web.bean.SelectList;

/**
 *
 * @author dongtv
 */
public class PaymentHistory extends BaseAction {

    private int search;
    private String date_from;
    private String date_to;
    private ArrayList<SelectList> searchList;
    private PaymentService payment;
    private List<FOrders> trans;

    public String index() {
        Date now = new Date();
        Date fromD = StringUtil.addDate(now, -30);// 1 month ago
        Date toD = StringUtil.addDate(now, 1);
        date_from = StringUtil.formatDisplayDate(fromD);
        date_to = StringUtil.formatDisplayDate(toD);
        logger.info("Get Payment History from: " + date_from + " to date:" + date_to);
        trans = payment.getOrderHistory(user.getId(), StringUtil.getDateForSQL(fromD), StringUtil.getDateForSQL(toD));
        return Action.INPUT;
    }

    @Override
    public String execute() {
        logger.info("Search service:" + search);
        logger.info("From Date:" + date_from);
        logger.info("To Date:" + date_to);
        request.setAttribute("sch", search);
        if (search == -1) {
            trans = payment.getOrderHistory(user.getId(), date_from.replaceAll("-", ""), date_to.replaceAll("-", ""));
        } else {
            trans = payment.getOrderHistory(user.getId(), date_from.replaceAll("-", ""), date_to.replaceAll("-", ""), search);
        }
        return Action.SUCCESS;
    }

    // <editor-fold defaultstate="collapsed" desc="set and get method">
    public String getDate_from() {
        return date_from;
    }

    public void setDate_from(String date_from) {
        this.date_from = date_from;
    }

    public String getDate_to() {
        return date_to;
    }

    public void setDate_to(String date_to) {
        this.date_to = date_to;
    }

    public int getSearch() {
        return search;
    }

    public void setSearch(int search) {
        this.search = search;
    }

    public ArrayList<SelectList> getSearchList() {
        searchList = new ArrayList<SelectList>();
        Iterator<FBillServices> billers = payment.getAllBillServices().iterator();
        searchList.add(new SelectList("-1", getText("user.show_bill.searchAll")));
        while (billers.hasNext()) {
            FBillServices biller = billers.next();
            searchList.add(new SelectList(String.valueOf(biller.getId()), biller.getServiceName()));
        }
        return searchList;
    }

    public void setSearchList(ArrayList<SelectList> searchList) {
        this.searchList = searchList;
    }

    public PaymentService getPayment() {
        return payment;
    }

    public void setPayment(PaymentService payment) {
        this.payment = payment;
    }

    public List<FOrders> getTrans() {
        return trans;
    }

    public void setTrans(List<FOrders> trans) {
        this.trans = trans;
    }
    // </editor-fold> 
}
